﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TJADRICD.DLL;
using System.IO;

namespace TJADRICD.CollaborativeDesignPlugin.Commission
{
    class DrawingSignList
   {
       public string drawingID;
       public string[] SignNameList;
       public DrawingSignList()
       {
           SignNameList = new string[CONST.enumCompetenceSize];
       }
       public void SetSignNameList(string[] _nameList)
       {
           if(_nameList.Length == CONST.enumCompetenceSize) SignNameList = _nameList;
       }
       public void SetDrawingID(string _id) { drawingID = _id; }
       public string[] GetSignNameList() { return SignNameList; }
   }

    class DBCompetence
   {
       public string userID;
       public string drawingID;
       public bool[] competence;
       public void SetCompetenceArray(bool[] _competence) { competence = _competence; }
       public string ToString()
       {
           string str = "";
           foreach (bool ch in competence)
           {
               str = str + (Convert.ToInt32(ch)).ToString();
           }
           return str;
       }
       public DBCompetence()
       {
           competence = new bool[CONST.enumCompetenceSize];
       }
       public void SetCompetence(int index)
       {
           if(index >= CONST.enumCompetenceSize) return;
           competence[index] = competence[index] || true;
       }
       
   }
    class CheckCompetence
    {
        
        static string proFirst = "000000000011111111111111";
        public bool[] getCompetence() { return cStr.getDistinguishStr(); }
        public int getEnumTypeSize() { return CONST.enumCompetenceSize; }
        private CompetenceStr cStr = new CompetenceStr();
        public string getCompetenceStr() { return cStr.ToString(); }
        public bool getCompetence(CONST.ConstCompetence targetSign) { return cStr.CompetenceDistinguish(targetSign); }
        public void SetDTCompetence(string competence)
        {

            cStr = cStr & competence;
            if (getCompetence(CONST.ConstCompetence.PROFESSIONAL_PERSON1) || getCompetence(CONST.ConstCompetence.PROFESSIONAL_PERSON2))
            {
                cStr = cStr & proFirst;

            }
        }
        public void InitComStr()
        {
            cStr.ClearCompetence();
        }
        private CompetenceStr CombineCompetence(CompetenceStr comStr, string commissionCompetence)
        {
            return (comStr & commissionCompetence);
        }
    }

    class Competence
    {

       public List<DBCompetence> DrawingToCompetence(DrawingSignList signList)
       {
           List<DBCompetence> dbCompetenceList = new List<DBCompetence>();
           string[] signNameList = signList.GetSignNameList();
           for (int i = 0; i < CONST.enumCompetenceSize; i++ )
           {
               DBCompetence dbCom = dbCompetenceList.Find(p => p.userID == signNameList[i]);
               if (dbCom != null)
               {
                   dbCom.SetCompetence(i);
               }
               else 
               {
                   DBCompetence dbNewCom = new DBCompetence();
                   dbNewCom.drawingID = signList.drawingID;
                   dbNewCom.userID = signNameList[i];
                   dbNewCom.SetCompetence(i);
                   dbCompetenceList.Add(dbNewCom);
               }
           }
           return dbCompetenceList;
       }

       static string proSecond = "000000000001010101010101";
       static string proFirst = "000000000010101010101010";
       
       public bool[] getCompetence(){return cStr.getDistinguishStr();}
       public int getEnumTypeSize() { return CONST.enumCompetenceSize; }
       public string getSignerName(CONST.ConstCompetence competenceIndex)
       {
           return signerNameList[Convert.ToInt32(competenceIndex)];
       }

       private CompetenceStr cStr = new CompetenceStr();
        private string[] signerNameList = new string[CONST.enumCompetenceSize];
        public void SetCompetence(string projectID, int count)
        {
            string[] fileName = projectID.Split('.');
            cStr.SetCompetenceStr(ReadDrawingCompetence(fileName[0]));

            CombineCompetence(cStr, ReadCommissionCompetence(fileName[0],count));
        }
        public string getCompetenceStr() { return cStr.ToString(); }
        public bool getCompetence(CONST.ConstCompetence targetSign) { return cStr.CompetenceDistinguish(targetSign); }
        public void SetDTCompetence(string competence)
        {

            cStr = cStr & competence;
            if (getCompetence(CONST.ConstCompetence.PROFESSIONAL_PERSON1))
            {
                    cStr = cStr & proFirst;

            }
            if(getCompetence(CONST.ConstCompetence.PROFESSIONAL_PERSON2))
            {
                cStr =cStr & proSecond;
            }
        }
        private string ReadDrawingCompetence(string projectID) {
            string str = "11001100110011001100110011001100110";
            string signer = "傅晓巍";
            FileStream fs = new FileStream("input.txt", FileMode.OpenOrCreate);
            StreamReader sr = new StreamReader(fs);
            string line = sr.ReadLine();
            sr.Close();
            fs.Close();
            string[] signerList = new string[36];
            if(line != null) signerList = line.Split(',');
            if (projectID == signerList[0])
            {
                DrawingSignList ds = new DrawingSignList();
                ds.drawingID = signerList[0];
                for (int i = 1; i < signerList.Length; i++)
                {
                    ds.SignNameList[i - 1] = signerList[i];
                }
                List<DBCompetence> sdb = DrawingToCompetence(ds);
                DBCompetence dbCom = sdb.Find(p => p.userID == signer);
                return dbCom.ToString();
            }
            for (int i = 0; i < getEnumTypeSize(); i++)
            {
                if ('1' == str[i])
                    signerNameList[i] = signer;
            }

            return str; 
        }
        private List<string> ReadCommissionCompetence(string projectID,int count) {
            List<string> nameList = new List<string>();
            List<string> signerList = new List<string>();
            #region 测试数据
            if (count == 0)
            {
                nameList.Add("00110000000000000000000000000000000");
                nameList.Add("00000000000000000000000000000001001");
                signerList.Add("王昌");
                signerList.Add("陈志东");
            }
            else if (1 == count)
            {
                nameList.Add("00110000000000000000000000000000000");
                signerList.Add("谭立明");
            }
            else if (2 == count)
            {
                nameList.Add("00000000000000000000000000000001001");
                signerList.Add("钱大勋");
            }
            else 
            {
                nameList.Add("00000000000000000000000000000001001");
                signerList.Add("DEFAULT");
            }
            #endregion

            for (int j = 0;j < nameList.Count;j++)
            {
                for (int i = 0; i < getEnumTypeSize(); i++)
                {
                    if ('1' == nameList[j][i])
                        signerNameList[i] = signerList[j];
                }
            }
            return nameList; 
        }
        public void InitComStr()
        {
            cStr.ClearCompetence();
        }
        private CompetenceStr CombineCompetence(CompetenceStr comStr, string commissionCompetence)
        {
            return (comStr & commissionCompetence);
        }
        private CompetenceStr CombineCompetence(CompetenceStr comStr, List<string> nameList)
        {
            foreach (string signer in nameList)
            {
                comStr = comStr & signer;
            }
            return comStr;
        }
    }

    class CompetenceStr
    {
        static int strSize = CONST.enumCompetenceSize;
        bool[] competenceDistinguishStr = new bool[strSize];
        public string LengthBalance(string str)
        {
            string standardStr = str;
            if (standardStr.Length > strSize)
            {
                standardStr = "";
                for (int i = 0; i < strSize; i++)
                {
                    standardStr = standardStr + str[i];
                }
            }
            else if (standardStr.Length < strSize)
            {
                for (int i = standardStr.Length; i < strSize; ++i)
                {
                    standardStr = standardStr + '0';
                }
            }
            return standardStr;
        }
        public void ClearCompetence()
        {
            for (int i = 0; i < strSize; i++)
                competenceDistinguishStr[i] = false;
        }
        public bool SetCompetenceStr(string strValue)
        {
            if (strValue.Length != strSize) return false;
            for(int i = 0; i < strSize; i++)
            {
                competenceDistinguishStr[i] = Convert.ToBoolean(Convert.ToInt32(strValue[i].ToString()));
            }
            return true;
        }
        public bool SetCompetenceStr(bool[] strValue)
        {
            if (strValue.Length != strSize) return false;
            competenceDistinguishStr = strValue;
            return true;
        }
        public bool[] getDistinguishStr() { return competenceDistinguishStr; }
        public string ToString()
        {
            string str = "";
            foreach (bool ch in competenceDistinguishStr)
            {
                str = str + (Convert.ToInt32(ch)).ToString();
            }
            return str;
        }
        public bool CompetenceDistinguish(CONST.ConstCompetence targetSign)
        {
            return competenceDistinguishStr[Convert.ToInt32(targetSign)];
        }
        public static CompetenceStr operator &(CompetenceStr comStr, string commissionCompetence)
        {
            if (commissionCompetence.Length != strSize) { return null; }
            CompetenceStr _comStr = new CompetenceStr();
            CompetenceStr _commissionStr = new CompetenceStr();
            _commissionStr.SetCompetenceStr(commissionCompetence);
            bool[] comChs = comStr.getDistinguishStr();
            bool[] compareChs = _commissionStr.getDistinguishStr();
            for (int i = 0; i < strSize; i++)
            {
                comChs[i] = comChs[i] || compareChs[i];
            }
            _comStr.SetCompetenceStr(comChs);
            return _comStr;
        }
    }
          
   class DBUserDrawingCompetence
    {
         public enum ENUMUserDrawingCompetence{
            userID,userName,projectID,subProjectID,profession,drawingID,competenceStr
         };

         public static int userDCEnumSize = Enum.GetNames(typeof(ENUMUserDrawingCompetence)).Length;
        public List<String[]> udcList = new List<String[]>();
   }

   class DrawingTree
    {
       DBUserDrawingCompetence udc = new DBUserDrawingCompetence();
       public UDCUserList udcList = new UDCUserList();
       string GetCompetenceStr(string userID, MySQLUserRole ur)
       {
           string str = "";
           if (ur.validation == userID) str = str + '1';
           else str = str + '0';
           if (ur.audit == userID) str = str + '1';
           else str = str + '0';
           if (ur.designManager1 == userID) str = str + '1';
           else str = str + '0';
           if (ur.designManager2 == userID) str = str + '1';
           else str = str + '0';
           if (ur.professionalPerson1 == userID) str = str + '1';
           else str = str + '0';
           if (ur.professionalPerson2 == userID) str = str + '1';
           else str = str + '0';
           if (ur.design1 == userID) str = str + '1';
           else str = str + '0';
           if (ur.design2 == userID) str = str + '1';
           else str = str + '0';
           if (ur.draw == userID) str = str + '1';
           else str = str + '0';
           if (ur.proof == userID) str = str + '1';
           else str = str + '0';
           if (str == "0000000000") return null;
           return str;
       }
       public void ReadDBUDC(string userID)
       {
           MySQLAuthorizeDao auth = new MySQLAuthorizeDao();
           List<string> userIDList = new List<string>();
           userIDList.Add(userID);
           List<MySQLAuthorize> authList = auth.GetAuthorize(userIDList);
           foreach (MySQLAuthorize au in authList)
           {
               userIDList.Add(au.majorID);
           }
           MySQLAccountDao acc = new MySQLAccountDao();
           List<MySQLAccount> accList = acc.GetAccountName(userIDList);
           MySQLUserRoleDao userRole = new MySQLUserRoleDao();
           List<MySQLUserRole> userRoleList = userRole.GetDrawingSignName(userIDList);

           CompetenceStr cstr = new CompetenceStr();
           foreach (MySQLUserRole ur in userRoleList)
           {
               foreach (MySQLAccount ac in accList)
               {
                   string comstr = GetCompetenceStr(ac.userId, ur);

                   if (comstr != null)
                   {

                       String[] udcItem = { ac.userId, ac.userName, ur.projectID, ur.subProjectID, ur.professionName, "", cstr.LengthBalance(comstr) };
                       udc.udcList.Add(udcItem);
                   }
               }
           }

           //#region 调试数据
           //bool[] t1 = new bool[CONST.enumCompetenceSize];

           //t1[Convert.ToInt32(CONST.ConstCompetence.AUDIT)] = true;
           //t1[Convert.ToInt32(CONST.ConstCompetence.VALIDATION)] = true;
           //t1[Convert.ToInt32(CONST.ConstCompetence.PROFESSIONAL_PERSON1)] = true;
           //t1[Convert.ToInt32(CONST.ConstCompetence.PROOF)] = true;
           //bool[] t2 = new bool[CONST.enumCompetenceSize];
           //t2[Convert.ToInt32(CONST.ConstCompetence.PROFESSIONAL_PERSON2)] = true;
           //t2[Convert.ToInt32(CONST.ConstCompetence.DRAW)] = true;
           //string str1 = "";
           //foreach (bool ch in t1)
           //{
           //    str1 = str1 + (Convert.ToInt32(ch)).ToString();
           //}
           //string str2 = "";
           //foreach (bool ch in t2)
           //{
           //    str2 = str2 + (Convert.ToInt32(ch)).ToString();
           //}
           //string[] udc1 = { "123", "傅晓巍", "999", "999", "建筑", "999-999-建筑-方案-001-A", str1 };
           //string[] udc2 = { "123", "傅晓巍", "999", "999", "结构", "999-999-建筑-方案-001-A", str1 };
           //string[] udc3 = { "123", "傅晓巍", "100", "100", "建筑", "999-999-建筑-方案-001-A", str1 };
           //string[] udc4 = { "123", "傅晓巍", "999", "999", "给排水", "999-999-建筑-方案-001-A", str1 };
           //string[] udc5 = { "120", "陈志东", "999", "999", "建筑", "999-999-建筑-方案-002-A", str1 };
           //string[] udc6 = { "120", "陈志东", "999", "999", "建筑", "999-999-建筑-方案-001-A", str2 };
           //string[] udc7 = { "120", "陈志东", "999", "999", "建筑", "999-999-建筑-方案-003-A", str1 };
           //string[] udc8 = { "120", "陈志东", "999", "999", "建筑", "999-999-建筑-方案-004-A", str1 };
           //string[] udc9 = { "122", "liuqing", "999", "999", "建筑", "999-999-建筑-方案-004-A", str2 };
           //string[] udc10 = { "122", "liuqing", "999", "999", "建筑", "999-999-建筑-方案-003-A", str2 };
           //string[] udc11 = { "122", "liuqing", "999", "999", "建筑", "999-999-建筑-方案-002-A", str2 };

           //udc.udcList.Add(udc1);
           //udc.udcList.Add(udc2);
           //udc.udcList.Add(udc3);
           //udc.udcList.Add(udc4);
           //udc.udcList.Add(udc5);
           //udc.udcList.Add(udc6);
           //udc.udcList.Add(udc7);
           //udc.udcList.Add(udc8);
           //udc.udcList.Add(udc9);
           //udc.udcList.Add(udc10);
           //udc.udcList.Add(udc11);
           //#endregion
       }
       public void DBToUDCList()
       {
           foreach (string[] u in udc.udcList)
           {
               udcList.InsertUserID(u);
           }
       }
    }
   #region UDC Class
   class UDCUserList
   {
      public  List<UDCUserID> udcList = new List<UDCUserID>();
      public void InsertUserID(string[] u)
      {
          UDCUserID uid = udcList.Find(p => p.userID == u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.userID)]);
          if(uid == null)
          {
              uid = new UDCUserID();
              uid.userID = u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.userID)];
              uid.userName = u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.userName)];
              uid.InsertProjectID(u);
              udcList.Add(uid);
          }
          else
              uid.InsertProjectID(u);
          
      }
   }
   class UDCUserID
   {
       public string userID;
       public String userName;
       public List<UDCProjectID> udcList = new List<UDCProjectID>();
       public void InsertProjectID(string[] u)
       {
           UDCProjectID pid = udcList.Find(p => p.projectID == u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.projectID)]);
           if (pid == null)
           {
               pid = new UDCProjectID();
               pid.projectID = u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.projectID)];
               pid.InsertSubProjectID(u);
               udcList.Add(pid);
           }
           else
               pid.InsertSubProjectID(u);

       }

   }
   class UDCProjectID
   {
       public string projectID;
       public List<UDCSubProjectID> udcList = new List<UDCSubProjectID>();
       public void InsertSubProjectID(string[] u)
       {
           UDCSubProjectID spid = udcList.Find(p => p.subProjectID == u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.subProjectID)]);
           if (spid == null)
           {
               spid = new UDCSubProjectID();
               spid.subProjectID = u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.subProjectID)];
               spid.InsertProfession(u);
               udcList.Add(spid);
               
           }
           else
                spid.InsertProfession(u);

       }

   }
    class UDCSubProjectID
    {
        public string subProjectID;
        public List<UDCProfession> udcList = new List<UDCProfession>();
        public void InsertProfession(string[] u)
        {
            UDCProfession pro = udcList.Find(p => p.profession == u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.profession)]);
            if (pro == null)
            {
                pro = new UDCProfession();
                pro.profession = u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.profession)];
                pro.drawingID = u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.drawingID)];
                pro.competenceStr = u[Convert.ToInt32(DBUserDrawingCompetence.ENUMUserDrawingCompetence.competenceStr)];
                udcList.Add(pro);
            }
        }
    }
    class UDCProfession
    {
        public string profession;
        
        public string drawingID;
        public string competenceStr;
    }
     #endregion
}
